const app = getApp();
var utils = require('../../untils/util.js');
Page({
  data: {
    userInfo: {},
    inComeList: [],
    objectInComeList: [],
    educationList: [],
    objeducationList: [],
    marriageList: [],
    objectMarriageList: [],
    jobList: [],
    objectJobList: [],
    regionList: [],
    objectRegionList: [],
    placeList: [],
    objectPlaceList: [],
    cregions: '',//城市
    xregions: '',//地区
    nativePlaces: '',
    zy: '',//职业
    zys: '',
    reqData: {
      name: '',
      idCard: '',
      nickName: '',
      height: '',
      weight: '',
      nativePlace: '',//籍贯号码
      // nativePlaces:'',
      money: '',//搜如
      // moneys:'',
      zy: '',//职业
      // zys:'',
      zyms: '',//具体职业
      education: '',//教育水平
      // educations:'',
      marry: '',//婚姻状态’
      cregion: '',//城市号码
      xregion: '',//地区号码
      // cregions: '',//城市
      // xregions: '',//地区
    }
  },
  onLoad: function () {
    wx.showLoading({
      title: '数据加载中...',
    })
    this.initPage();
  },
  initPage: function () {
    
    var _this = this;
    app.doReq(app.globalData.DICTIONARY, {
      parentId: '1010,6,8,418,975'//职业,婚姻状态,学历,月薪范围,省份
    }, 'get', function (res) {
      var data = res.data;
      if (data.code == 200) {
        var incomeList = data.obj[418];
        var incomeListTemp = [];
        for (let i of incomeList) {
          incomeListTemp.push(i.name);
        }
        var marriageList = data.obj[6];
        var marriageListTemp = [];
        for (let i of marriageList) {
          marriageListTemp.push(i.name);
        }
        var educationList = data.obj[8];
        var educationListTemp = [];
        for (let i of educationList) {
          educationListTemp.push(i.name);
        }
        var jobList = data.obj[1010];
        var jobListTemp = [];
        for (let i of jobList) {
          jobListTemp.push(i.name);
        }
        var regionList = data.obj[975];
        var regionListTemp = [];
        for (let i of regionList) {
          regionListTemp.push(i.name);
        }

        _this.setData({
          inComeList: incomeListTemp,
          objectInComeList: incomeList,
          educationList: educationListTemp,
          objeducationList: educationList,
          marriageList: marriageListTemp,
          objectMarriageList: marriageList,
          jobList: jobListTemp,
          objectJobList: jobList,
          regionList: regionListTemp,
          objectRegionList: regionList
        },function(){
          wx.hideLoading();
        })
      }
    });
    //刷新用户信息
    app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function (v) {
      var userInfo = wx.getStorageSync('userInfo');
      _this.setData({
        nativePlaces: userInfo.member.nativePlaces,
        moneys: userInfo.member.moneys,
        zys: userInfo.member.zys,
        educations: userInfo.member.educations,
        marrys: userInfo.member.marrys,
        cregions: userInfo.member.cregion.name,
        xregions: userInfo.member.xregion.name,
        reqData: {
          id: app.globalData.userid,
          name: userInfo.member.name,
          idCard: userInfo.member.idCard,
          nickName: userInfo.member.nickName,
          height: userInfo.member.height,
          weight: userInfo.member.weight,
          nativePlace: userInfo.member.nativePlace,//籍贯
          // nativePlaces: userInfo.member.nativePlaces,
          money: userInfo.member.money,//搜如
          // moneys: userInfo.member.moneys,
          zy: userInfo.member.zy,//职业
          // zys: userInfo.member.zys,
          zyms: userInfo.member.zyms,//具体职业
          education: userInfo.member.education,//教育水平
          // educations: userInfo.member.educations,
          marry: userInfo.member.marry,//婚姻状态’
          // marrys: userInfo.member.marrys,
          cregion: userInfo.member.cregion.id,//城市号码
          xregion: userInfo.member.xregion.id,//地区号码
          company: userInfo.member.company
          // cregions: userInfo.member.cregion.name,//城市
          // xregions: userInfo.member.xregion.name,//地区
        }
      })
    }).then(function (v) {
      //获得地区信息
      // var cityList = [];
      // var cityListTemp = [];
      // var areaList = [];
      // var areaListTemp = [];
      app.doReqSyn(app.globalData.COMMON.ADDRLIST, {
        parentId: 2
      }, 'get').then(function (res) {
        var data = res.data;
        if (data.code == 200) {
          var cityList = data.obj[2];
          var cityListTemp = [];
          for (let i of cityList) {
            cityListTemp.push(i.name);
          }
          return { cityList: cityList, cityListTemp: cityListTemp };
          // _this.setData({
          //   placeList: [cityListTemp],
          //   objectPlaceList: [cityList]
          // })
        }
      }).then(function (res) {
        let cityList = res.cityList;
        let cityListTemp = res.cityListTemp;
        app.doReqSyn(app.globalData.COMMON.ADDRLIST, {
          parentId: _this.data.reqData.cregion
        }, 'get').then(function (res) {
          let data = res.data;
          if (data.code == 200) {
            var areaList = data.obj[_this.data.reqData.cregion];
            var areaListTemp = [];
            for (let i of areaList) {
              areaListTemp.push(i.name);
            }
            _this.setData({
              placeList: [cityListTemp, areaListTemp],
              objectPlaceList: [cityList, areaList]
            })
          }
        });
      });
    })
  },
  whenInput: function (e) {
    var v = e.detail.value;
    var nodeType = e.currentTarget.dataset.nodetype;
    if (nodeType == 'name') {
      // this.data.userInfo.member.name = v;
      this.data.reqData.name = v
    } else if (nodeType == 'idCard') {
      // this.data.userInfo.member.idCard = v;
      this.data.reqData.idCard = v
    } else if (nodeType == 'nickName') {
      // this.data.userInfo.member.nickName = v;
      this.data.reqData.nickName = v
    } else if (nodeType == 'height') {
      this.data.reqData.height = v
      // this.data.userInfo.member.height = v;
    } else if (nodeType == 'weight') {
      this.data.reqData.weight = v
      // this.data.userInfo.member.weight = v;
    } else if (nodeType == 'zyms') {
      this.data.reqData.zyms = v
      // this.data.userInfo.member.zyms = v;
    } else if (nodeType == 'company') {
      this.data.reqData.company = v
      // this.data.userInfo.member.zyms = v;
    }
  },
  whenChage: function (e) {
    var v = e.detail.value;//数组
    console.log(e);
    console.log(v);
    var nodeType = e.currentTarget.dataset.nodetype;
    if (nodeType == 'region') {
      let v1 = v[0] != null ? v[0] : 0;
      let v2 = v[1] != null ? v[1] : 0;
      this.setData({
        // cregion: this.data.objectPlaceList[0][v1].id,
        // xregion: this.data.objectPlaceList[1][v2].id,
        cregions: this.data.objectPlaceList[0][v1].name,
        xregions: this.data.objectPlaceList[1][v2].name
      });
      this.data.reqData.cregion = this.data.objectPlaceList[0][v1].id
      this.data.reqData.xregion = this.data.objectPlaceList[1][v2].id;
      // this.data.reqData.cregions = this.data.objectPlaceList[0][v2].name
      // this.data.reqData.xregions = this.data.objectPlaceList[1][v2].name
    } else if (nodeType == 'nativePlaces') {
      this.data.reqData.nativePlace = this.data.objectRegionList[v].id;
      this.setData({
        nativePlaces: this.data.objectRegionList[v].name
      });
      // this.data.reqData.nativePlaces = this.data.objectRegionList[v].name;
    } else if (nodeType == 'job') {
      this.data.reqData.zy = this.data.objectJobList[v].id;
      this.setData({
        zys: this.data.objectJobList[v].name
      });
      // this.data.reqData.zys = this.data.objectRegionList[v].name;
    } else if (nodeType == 'moneys') {
      this.data.reqData.money = this.data.objectInComeList[v].id;
      this.setData({
        moneys: this.data.objectInComeList[v].name
      });
      // this.data.reqData.moneys = this.data.objectInComeList[v].name;
    } else if (nodeType == 'educations') {
      this.data.reqData.education = this.data.objeducationList[v].id;
      this.setData({
        educations: this.data.objeducationList[v].name
      });
      // this.data.reqData.educations = this.data.objeducationList[v].name;
    } else if (nodeType == 'marrys') {
      this.data.reqData.marry = this.data.objectMarriageList[v].id;
      this.setData({
        marrys: this.data.objectMarriageList[v].name
      });
      // this.data.reqData.marrys = this.data.objectMarriageList[v].name;
    }
  },
  whenCityChange: function (e) {
    var _this = this;
    var colum = e.detail.column;
    var cityIndex = e.detail.value;
    var cityId = _this.data.objectPlaceList[0][cityIndex].id;
    if (colum == 0) {
      app.doReq(app.globalData.COMMON.ADDRLIST, {
        parentId: cityId
      }, 'get', function (res) {
        let data = res.data;
        if (data.code == 200) {
          let areaList = data.obj[cityId];
          let areaListTemp = [];
          for (let i of areaList) {
            areaListTemp.push(i.name);
          }
          _this.setData({
            placeList: [_this.data.placeList[0], areaListTemp],
            objectPlaceList: [_this.data.objectPlaceList[0], areaList]
          })
        }
      });
    }
  },
  doSave: function (e) {
    var _this = this;
    console.log(this.data.reqData)
    let data = this.data.reqData
    let req = {};
    for (let k in data) {
      if (data[k] == '' || data[k] == null || data[k] == undefined) {
        wx.showToast({
          title: '请检查带星号为必填项！',
          icon: 'none'
        });
        return
      }
      if (k == 'cregion') {
        req['xgmember.cregion.id'] = data['cregion'];
      } else if (k == 'xregion') {
        req['xgmember.xregion.id'] = data['xregion'];
      } else if (k == 'id') {
        req['id'] = data['id'];
      } else {
        req['xgmember.' + k] = data[k];
      }
    }
    
    if(utils.idCardValid(data.idCard)){
      app.doReq(app.globalData.SERVICE.USERCENTER.BASIC_INFO_JB_SAVE, req, 'get',
        function (res) {
          var data = res.data;
          if (data.code == 200 && data.type == 'SUCCESS') {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            });
            app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function (v) {
              var pages = getCurrentPages();
              var currentPage = pages[pages.length - 1];//当前页面
              var pervPagge = pages[pages.length - 2];//上个页面
              pervPagge.setData({
                reloadFlag: true,
              })
              wx.navigateBack({});
            });
          }
        });
    }
    
  },
  whenFocusInput: function (e) {

  },
  doCancel:function(){
    wx.navigateBack({})
  }
})
